package com.zhang.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.common.util.time.TimeUtils;

import com.zhang.system.entity.Menu;
import com.zhang.system.enumerate.PermissionTypeEnum;
import com.zhang.system.mapper.MenuMapper;
import com.zhang.system.service.IMenuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限菜单表表 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2023-07-06
 */

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {


    @Override
    public Long addMenu(Menu menu) {
        menu.setCreateTime(TimeUtils.getLocalDateTimeStr());
        baseMapper.insert(menu);
        return menu.getId();
    }

    @Override
    public List<Menu> getAllMenusToTree() {
        return getMenuTree(list());
    }
    @Override
    public List<Menu> getMenuTree(List<Menu> menus) {
        List<Menu> collect = menus.stream().filter(item -> item.getParentId() == 0).collect(Collectors.toList());
        return collect.stream().peek(item -> {
            List<Menu> treeFromMenus = setMenusToTree(menus, item.getId());
            item.setChildren(treeFromMenus);
        }).collect(Collectors.toList());
    }

    @Override
    public IPage getAllMenusByPageAndCondition(Integer pageNum, Integer pageSize, Map searchMenuDto) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        String searchValue=String.valueOf(searchMenuDto.get("searchValue"));
        if (!StrUtil.isBlank(searchValue)){
            wrapper.like("name",searchValue).
                    or().like("menu_code",searchValue)
                    .or().eq("id",searchValue);
        }
        String nodeType=String.valueOf(searchMenuDto.get("nodeType"));
        if (!StrUtil.isBlank(nodeType)){
            wrapper.eq("node_type",nodeType);
        }
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public List<Menu> getDirectoryPermissionByTree() {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getNodeType, PermissionTypeEnum.directory.getCode());
        List<Menu> list = list(wrapper);
        return getMenuTree(list);
    }



    /*
     * 设置菜单树
     *
     * @param allMenus 所有菜单
     * @param menuId   当前遍历的菜单id
     * @return {@link List}<{@link Menu}>
     */

    public List<Menu> setMenusToTree(List<Menu> allMenus, Long menuId) {
        return allMenus.stream().filter(item ->
                item.getParentId().equals(menuId)
        ).map(item -> {
            List<Menu> treeFromMenus = setMenusToTree(allMenus, item.getId());
            item.setChildren(treeFromMenus);
            return item;
        }).collect(Collectors.toList());
    }
}
